package com.taosdata.jdbc.springbootdemo.config;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author weir
 *
 *         2019年6月5日 下午5:20:56
 */
@Configuration
public class AsyncConfig implements AsyncConfigurer {
	private static final Logger LOGGER = LoggerFactory.getLogger(AsyncConfig.class);
	@Value("${thread.pool.corePoolSize:10}")
	private int corePoolSize;

	@Value("${thread.pool.maxPoolSize:20}")
	private int maxPoolSize;

	@Value("${thread.pool.keepAliveSeconds:4}")
	private int keepAliveSeconds;

	@Value("${thread.pool.queueCapacity:512}")
	private int queueCapacity;

//	@Bean(name = "scorePoolTaskExecutor")
	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(corePoolSize);
		executor.setMaxPoolSize(maxPoolSize);
		executor.setKeepAliveSeconds(keepAliveSeconds);
		executor.setQueueCapacity(queueCapacity);
		executor.setRejectedExecutionHandler(
				(Runnable r, ThreadPoolExecutor exe) -> {
					LOGGER.warn("当前任务线程池队列已满.");
				});
		executor.initialize();
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return new AsyncUncaughtExceptionHandler() {
			@Override
			public void handleUncaughtException(Throwable ex, Method method,
					Object... params) {
				LOGGER.error("线程池执行任务发生未知异常.", ex);
			}
		};
	}
}
